/*
 * @lc app=leetcode.cn id=56 lang=cpp
 *
 * [56] 合并区间
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    vector<vector<int>> merge(vector<vector<int>> &intervals)
    {
        vector<vector<int>> ans;

        sort(intervals.begin(), intervals.end(), [](vector<int> &l, vector<int> &r)
             { return l[0] < r[0]; });

        vector<int> &curr_v = intervals[0];
        for (int i = 1; i < intervals.size(); i++)
        {
            if (intervals[i][0] <= curr_v[1])
            {
                curr_v[1] = max(curr_v[1], intervals[i][1]);
            }
            else
            {
                ans.push_back(curr_v);
                curr_v = intervals[i];
            }
        }
        {
            ans.push_back(curr_v);
        }

        return ans;
    }
};
// @lc code=end
